package com.qfedu.controller;

import com.qfedu.common.JsonResult;
import com.qfedu.entity.User;
import com.qfedu.service.UserService;
import com.qfedu.utils.StrUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import javax.servlet.http.HttpSession;
import javax.sound.midi.Soundbank;

/**
 * projectName: carCourier
 * author: CGY
 * time: 2020/9/17 14:29
 * description:用户控制器
 */
@Controller
public class LoginController {
    @Autowired
    private UserService userService;

    /**
     * 登陆功能
     * @param tel
     * @param password
     * @param session
     * @return
     */
    @RequestMapping("/login.do")
    @ResponseBody
    public JsonResult login(String tel, String password, HttpSession session){
        User login = userService.login(tel, password);
        session.setAttribute("loginUser",login);
        JsonResult jsonResult = new JsonResult(1, login);
        return jsonResult;
    }

    /**
     *注册功能实现
     * @param tel
     * @param password
     * @param email
     * @param invitation
     * @return
     */
    @RequestMapping("/register.do")
    @ResponseBody
    public JsonResult registerUser(String tel,String password,String email,String invitation){
        userService.register(tel,password,invitation,email);
        User user = userService.login(tel, password);
        return new JsonResult(1,user);
    }

    /**
     *获取登陆用户信息
     * @param session
     * @return
     */
    @RequestMapping("query.do")
    @ResponseBody
    public JsonResult query(HttpSession session){
        User user = (User) session.getAttribute("loginUser");
        if (user == null){
            return new JsonResult(0,"未登录");
        }else {
            return new JsonResult(1,user);
        }
    }

    /**
     *获取用户手机信息
     * @param session
     * @return
     */
    @RequestMapping("/loginStusta.do")
    @ResponseBody
    public JsonResult loginStusta(HttpSession session){
        User user = (User) session.getAttribute("loginUser");
        return new JsonResult(1,user.getTel());
    }
}

